package com.testflow.hyh.testing;

import com.alibaba.fastjson.JSON;
import com.testflow.hyh.annotation.PackApiDesc;
import com.testflow.hyh.annotation.TestApiDesc;
import com.testflow.hyh.constats.Constants;
import com.testflow.hyh.entity.LoginDto;
import com.testflow.hyh.enums.MethodType;
import com.testflow.hyh.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.testng.Reporter;
import org.testng.annotations.Test;

import java.text.SimpleDateFormat;


@Slf4j
@Component
@PackApiDesc(packName = "普拉达接口",packAuthor = "何宇航",packCreateTime = "2022-06-16")
public class LoginTesting{

    public static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @TestApiDesc(apiName = "远端登录接口", apiUrl = Constants.REMOTE_URL, apiMethod = MethodType.POST, apiRemark = "测试远端用例调查")
    @Test
    public void 远端接口调用(){
        LoginDto loginDto = new LoginDto("duanwei111","woaiduanwei");
        Reporter.log("入参：【"+JSON.toJSONString(loginDto)+"】");
        String loginResponse = HttpUtil.commonPostRequest(Constants.REMOTE_URL+"login", JSON.toJSONString(loginDto));
        Reporter.log("出参：【"+JSON.toJSONString(loginResponse)+"】");
    }

//    @AfterMethod
//    public void 方法切入(ITestResult result){
//        //获取上一个方法执行的log日志
//        List<String> output = Reporter.getOutput(result);
//        //获取方法载体
//        ConstructorOrMethod constructorOrMethod = result.getMethod().getConstructorOrMethod();
//        //判断当前方法上是否包含@TestApiDesc注解，如果包含此注解，那么解析注解所有参数的值
//        if(constructorOrMethod.getMethod().isAnnotationPresent(TestApiDesc.class)){
//            List<ReportApiResponse> apiResponses = new ArrayList<>();
//            for (String response:output) {
//                ReportApiResponse reportApiResponse = new ReportApiResponse();
//                reportApiResponse.setState(result.getStatus());
//                reportApiResponse.setElapseTime(result.getEndMillis()-result.getStartMillis());
//                reportApiResponse.setResponse(response);
//                apiResponses.add(reportApiResponse);
//            }
//            //解析TestApiDesc注解中的值
//            TestApiDesc testApiDesc = constructorOrMethod.getMethod().getAnnotation(TestApiDesc.class);
//            ReportApiRequest request = new ReportApiRequest();
//            request.setApiName(testApiDesc.apiName())
//                   .setApiUrl(testApiDesc.apiUrl())
//                   .setApiTestState(result.getStatus())
//                   .setApiMethod(testApiDesc.apiMethod().toString())
//                   .setTestInterfaceStartTime(sdf.format(new Date(result.getStartMillis())))
//                   .setTestInterfaceEndTime(sdf.format(new Date(result.getEndMillis())))
//                   .setTestInterfaceTotalTime(result.getEndMillis()-result.getStartMillis())
//                   .setTestCustomRemark(testApiDesc.apiRemark())
//                   .setApiResponse(apiResponses);
//            TestManager.requestList.add(request);
//        }
//    }
//
//    @AfterTest
//    public void 合并结果集(){
//        ReportInfo reportInfo = new ReportInfo();
//        reportInfo.setTestSuccess(TestManager.successList.size());
//        reportInfo.setTestFail(TestManager.errorList.size());
//        reportInfo.setRequestList(TestManager.requestList);
//        log.info("【成功的用例：{} 个】", TestManager.successList.size());
//        log.info("【失败的用例：{} 个】", TestManager.errorList.size());
//        //发送邮件
//        MailUtils.send163Mail("duanwei0412@163.com","测试结果集",reportInfo);
//    }
}
